package com.atguigu.dao.impl;

import com.atguigu.dao.OrderDao;
import com.atguigu.pojo.Book;
import com.atguigu.pojo.Order;
import com.sun.org.apache.xpath.internal.operations.Or;

import java.util.List;

/**
 * @author YAO
 * @create 2021-05-13 9:40
 */
public class OrderDaoImpl extends BaseDao implements OrderDao {

    @Override
    public int saveOrder(Order order) {
        String sql = "insert into t_order(`order_id`,`create_time`,`price`,`status`,`user_id`) values(?,?,?,?,?)";
        return update(sql,order.getOrderId(),order.getCreateTime(),order.getPrice(),order.getStatus(),order.getUserId());
    }

    @Override
    public List<Order> queryOrders() {
        String sql = "select `order_id` orderId, create_time createTime, price, status,user_id userId" +
                " from t_order";
        return queryForList(Order.class,sql);
    }

    @Override
    public int changeOrderStatus(String orderId, Integer status) {
        String sql = "update t_order set `status`=? where `order_id`=?";
        return update(sql,status,orderId);
    }

    @Override
    public List<Order> queryOrdersByUserId(Integer userId) {
        String sql = "select `order_id` orderId, create_time createTime, price, status,user_id userId" +
                " from t_order where user_id=?";
        return queryForList(Order.class,sql,userId);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select count(*) from t_order";

        Number cnt = (Number) queryForSingleValue(sql);

        return cnt.intValue();
    }

    @Override
    public List<Order> queryForPageItems(int begin, Integer pageSize) {
        String sql = "select `order_id` orderId, create_time createTime, price, status,user_id userId" +
                " from t_order limit ?,?";
        return queryForList(Order.class,sql,begin,pageSize);
    }

    @Override
    public void deleteOrdersByUserId(Integer userId) {
        String sql = "DELETE FROM t_order where user_id=?";
        update(sql,userId);
    }

    @Override
    public void deleteOrderItemsByOrderId(String orderId) {
        String sql = "DELETE FROM t_order_item where order_id=?";
        update(sql,orderId);
    }

    @Override
    public void deleteOrderByOrderId(String orderId) {
        String sql = "DELETE FROM t_order where order_id=?";
        update(sql,orderId);
    }


}
